/**
 *  This file is part of AugeasWH.
 *
 *  AugeasWH. is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AugeasWH. is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AugeasWH.  If not, see <http://www.gnu.org/licenses/>.
 */

package cz.fit.vutbr.pis2012.augeaswh.utils;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public final class FacesUtils {

	/**
	 * Konstruktor
	 */
	private FacesUtils() {}
	
	/**
	 * Prida FacesMessage do aktualneho faces kontextu
	 * @param context kontext
	 * @param message sprava
	 * @return FacesMessage objekt
	 */
	public static void addFacesMessage(String context, String message) {
		FacesContext.getCurrentInstance().addMessage(context, new FacesMessage(message));
	}
	
	/**
	 * Vlozi do session zvoleny objekt pod zvolenym klucom. Ak uz kluc existuje predosly objekt je nahradeny novym
	 * @param key kluc
	 * @param obj objekt
	 */
	public static void setSessionObject(String key, Object obj) {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest httpRequest = (HttpServletRequest) context.getExternalContext().getRequest();
		HttpSession session = httpRequest.getSession(false);
		
		if (session == null)
			return;
		session.setAttribute(key, obj);
	}
	
	/**
	 * Odstrani session objekt z danym klucom
	 * @param key kluc
	 */
	public static void removeSessionObject(String key) {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest httpRequest = (HttpServletRequest) context.getExternalContext().getRequest();
		HttpSession session = httpRequest.getSession(false);
		
		if (session == null) 
			return;
		session.removeAttribute(key);
	}
	
}
